package com.xj.admin.base.upload.web;

import com.baomidou.mybatisplus.mapper.Condition;
import com.xj.admin.base.index.web.BaseController;
import com.xj.admin.bussiness.deviceInformationImg.service.ITbDeviceInformationImgService;
import com.xj.admin.bussiness.personalInformationImg.service.ITbPersonalInformationImgService;
import com.xj.admin.util.ImageUploadUtil;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.base.common.exception.EnumSvrResult;
import com.xj.common.bussiness.deviceInformation.entity.TbDeviceInformation;
import com.xj.common.bussiness.deviceInformationImg.entity.TbDeviceInformationImg;
import com.xj.common.bussiness.personalInformationImg.entity.TbPersonalInformationImg;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 上传图片 前端控制器
 * </p>
 *
 * @author xj
 * @since 2016-12-20
 */
@Controller
@RequestMapping("/upload/")
public class UploadController extends BaseController {
    private Logger logger = LogManager.getLogger(UploadController.class.getName());

    @Autowired
    private ITbPersonalInformationImgService personalInformationImgService;
    @Autowired
    private ITbDeviceInformationImgService deviceInformationImgService;

    @RequestMapping("ckUploadImg")
    public void ckUpdateImg(HttpServletRequest request, HttpServletResponse response) {
        String DirectoryName = "upload/ck/";
        try {
            ImageUploadUtil.ckeditor(request, response, DirectoryName);

        } catch (Exception e) {
            logger.error("CK上傳失敗：{}", e.getMessage());
        }

    }

    @RequestMapping("uploadImg")
    @ResponseBody
    public AbstractBean updateImg(HttpServletRequest request) {
        String DirectoryName = "https://api.gcjxglzx.com/images/cms-admin-upload/";
        try {
            String fileName = ImageUploadUtil.upload(request, DirectoryName);
            return json(DirectoryName + fileName);
        } catch (Exception e) {
            logger.error("上傳失敗：{}", e.getMessage());
            return fail(EnumSvrResult.ERROR_UPLOAD_IMG);
        }
    }

    //备案设备信息照片
    @RequestMapping("updateDeviceImg")
    @ResponseBody
    public AbstractBean updateDeviceImg(HttpServletRequest request,String num) {
        String DirectoryName = "https://api.gcjxglzx.com/images/cms-admin-upload/";
        //Map<String, Object> parameters = null;
        System.out.println("+++++++"+num);
        //添加各种照片信息
        try {
            String fileName = ImageUploadUtil.upload(request, DirectoryName);
            String url = DirectoryName + fileName + ",";
            TbDeviceInformationImg tbDeviceImg = new TbDeviceInformationImg();
            tbDeviceImg.setReferenceDeviceImg(url);
            deviceInformationImgService.update(tbDeviceImg, Condition.create().eq("big_shelf_num", num));
            return json(DirectoryName + fileName);
        } catch (Exception e) {
            logger.error("上傳失敗：{}", e.getMessage());
            return fail(EnumSvrResult.ERROR_UPLOAD_IMG);
        }
    }

    //备案用户信息照片
    @RequestMapping("updateUserImg")
    @ResponseBody
    public AbstractBean updateUserImg(HttpServletRequest request, TbDeviceInformation tbdeviceinformation) {
        String DirectoryName = "https://api.gcjxglzx.com/images/cms-admin-upload/";
        try {
            String fileName = ImageUploadUtil.upload(request, DirectoryName);
            //System.out.println("request:"+request);
            String url = DirectoryName + fileName + ",";
            TbPersonalInformationImg tbPersonalInformationImg = new TbPersonalInformationImg();
            TbDeviceInformationImg tbDeviceInformationImg = new TbDeviceInformationImg();
            tbDeviceInformationImg.setIdentityNum(tbdeviceinformation.getIdentification());
            List<TbDeviceInformationImg> deviceInformationImgList = deviceInformationImgService.getTbdeviceInformationImg(tbDeviceInformationImg);
            deviceInformationImgList.get(0).setReferenceDeviceImg(url);
            personalInformationImgService.insert(tbPersonalInformationImg);
            return json(DirectoryName + fileName);
        } catch (Exception e) {
            logger.error("上傳失敗：{}", e.getMessage());
            return fail(EnumSvrResult.ERROR_UPLOAD_IMG);
        }
    }

    //备案发票信息照片
    @RequestMapping("updateInvoiceImg")
    @ResponseBody

    public AbstractBean updateInvoiceImg(HttpServletRequest request) {
        String DirectoryName = "https://api.gcjxglzx.com/images/cms-admin-upload/";
        try {
            String fileName = ImageUploadUtil.upload(request, DirectoryName);
            System.out.println("返回地址:" + DirectoryName + fileName);
            String url = DirectoryName + fileName + ",";
            TbPersonalInformationImg tbPersonalInformationImg = new TbPersonalInformationImg();
            tbPersonalInformationImg.setReferenceInvoiceImg(url);
            personalInformationImgService.insert(tbPersonalInformationImg);
            return json(DirectoryName + fileName);
        } catch (Exception e) {
            logger.error("上傳失敗：{}", e.getMessage());
            return fail(EnumSvrResult.ERROR_UPLOAD_IMG);
        }
    }
}
